﻿using System;
using System.Collections.Generic;
using System.Text;


namespace TRPOP.CMS.Model
{
    /// <summary>
    /// 数据表字段配置方法
    /// </summary>
    public class TableFieldInfo
    {
        /// <summary>
        /// 根据数据字段类型 长度 返回类型长度限制的sql语句
        /// </summary>
        /// <param name="strType"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetLengthLimit(string strType, int length)
        {
            string strReturn = "";
            if (SystemInfoHelper.getSystemInfo().NameSpaceForCMSDAL.ToLower().Contains("sqlserver"))
            {
                if (length <= 0)
                    return "";
                switch (strType.Trim().ToLower())
                {
                    case "varchar":
                        if (length > 250)
                            length = 250;
                        strReturn = "(" + length.ToString() + ")";
                        break;
                    case "nvarchar":
                        if (length > 250)
                            length = 250;
                        strReturn = "(" + length.ToString() + ")";
                        break;
                    default: // bit int text  
                        break;
                }
            }
            return strReturn;
        }

        /// <summary>
        /// 根据数据类型 值 返回default值限制的sql
        /// </summary>
        /// <param name="strType"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string PutDeafultValue(string strType, string  val)
        {
            string strReturn = "";
            if (string.IsNullOrEmpty(val))
                return "";
            if (SystemInfoHelper.getSystemInfo().NameSpaceForCMSDAL.ToLower().Contains("sqlserver"))
            {
                switch (strType.Trim().ToLower())
                {
                    case "varchar":
                        strReturn = "N'" + val.Replace("'", "''") + "'";
                        break;
                    case "nvarchar":
                        strReturn = "N'" + val.Replace("'", "''") + "'";
                        break;
                    case "text":
                        strReturn = "N'" + val.Replace("'", "''") + "'";
                        break;
                    default: // bit int text  
                        break;
                }
            }
            return strReturn; 
        }

        /// <summary>
        /// 根据数值类型 判断是否允许搜索
        /// </summary>
        /// <param name="strType"></param>
        /// <returns></returns>
        public static bool IsSearch(string strType)
        {
            bool blnRetrun = false;
            switch (strType.Trim().ToLower().Trim())
            {
                case "varchar":
                    blnRetrun = true;
                    break;
                case "nvarchar":
                    blnRetrun = true;
                    break;
                case "text":
                    blnRetrun = true;
                    break;
                default:
                    blnRetrun = false;
                    break;
            }
            return blnRetrun;
        }
    }
}
